<template>
  <div class="cateList_main">
    <el-card>
      <Bread></Bread>
      <div class="addBtnLine">
        <el-button
          type="primary"
          size="small"
          class="addBtn"
          @click="addCateList"
          >添加分类</el-button
        >
      </div>
      <!-- 文章分类列表 -->
      <el-table :data="cateList" style="width: 100%" border>
        <el-table-column type="index" label="序号" width="180">
        </el-table-column>
        <el-table-column prop="cate_name" label="分类名称" width="180">
        </el-table-column>
        <el-table-column prop="cate_alias" label="分类别名"> </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="success" @click="editFn(scope.row)"
              >修改</el-button
            >
            <!-- 删除列表 -->
            <el-button type="danger" @click="deleteFn(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 添加分类弹出对话框 -->
    <!-- 设置整个对话框宽度width -->
    <el-dialog
      title="添加文章分类"
      :visible.sync="dialogFormVisible"
      width="40%"
      @close="resetAddForm"
    >
      <!-- 给表单添加rule属性校验 每条表单上添加对应prop属性 -->
      <el-form :model="form" :rules="rules" ref="addForm">
        <el-form-item label="分类名称" label-width="90px" prop="cate_name">
          <el-input
            v-model="form.cate_name"
            autocomplete="off"
            placeholder="请输入分类名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="分类别名" label-width="90px" prop="cate_alias">
          <el-input
            v-model="form.cate_alias"
            autocomplete="off"
            placeholder="请输入分类别名"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addBtn">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 添加分类弹出对话框 -->

    <!-- 编辑分类弹出对话框 -->
    <el-dialog
      title="编辑文章分类"
      :visible.sync="dialogEditFormVisible"
      width="40%"
      @close="resetEditForm"
    >
      <!-- 给表单添加rule属性校验 每条表单上添加对应prop属性 -->
      <el-form :model="formEdit" :rules="rules" ref="EditForm">
        <el-form-item label="分类名称" label-width="90px" prop="cate_name">
          <el-input
            v-model="formEdit.cate_name"
            autocomplete="off"
            placeholder="请输入分类名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="分类别名" label-width="90px" prop="cate_alias">
          <el-input
            v-model="formEdit.cate_alias"
            autocomplete="off"
            placeholder="请输入分类别名"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogEditFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="editConfirmBtn">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 编辑分类弹出对话框 -->
  </div>
</template>

<script>
import { addCateApi, deleteCateByIdApi, editCateByIdApi } from '../../api/art'
import { mapActions, mapState } from 'vuex'
export default {
  data () {
    return {
      dialogFormVisible: false,
      form: {
        cate_name: '',
        cate_alias: ''
      },
      // 编辑按钮
      dialogEditFormVisible: false,
      formEdit: {
        cate_name: '',
        cate_alias: ''
      },
      // 添加校验
      rules: {
        cate_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          {
            pattern: /^\S{1,10}$/,
            message: '分类名称必须是1-10位的非空字符串',
            trigger: 'blur'
          }
        ],
        cate_alias: [
          { required: true, message: '请输入分类别名', trigger: 'blur' },
          {
            pattern: /^[a-zA-Z0-9]{1,15}$/,
            message: '分类别名必须是1-15位的字母数字',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created () {
    this.AgetCateList()
  },
  computed: {
    ...mapState('art', ['cateList'])
  },
  methods: {
    ...mapActions('art', ['AgetCateList']),
    addCateList () {
      this.dialogFormVisible = true
    },

    // 弹出对话框校验
    addBtn () {
      this.$refs.addForm.validate(async (valid) => {
        if (valid) {
          //  校验成功
          // 2、发送请求 api
          const res = await addCateApi(this.form)
          console.log(res)
          // 拿到数据之后判断  如果code=0表示添加成功
          if (res.data.code === 0) {
            this.$message({
              message: '添加成功!',
              type: 'success'
            })
            //  3、刷新页面
            this.AgetCateList()
            // 4.关闭对话框
            this.dialogFormVisible = false
          }
        }
      })
    },
    // 重置输入框
    resetAddForm () {
      this.$refs.addForm.resetFields()
    },
    // 根据id删除文章分类
    deleteFn (id) {
      this.$confirm('您确定要删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          // 点击确定按钮  发送请求
          const res = await deleteCateByIdApi(id)
          console.log(res)
          // 更新页面
          if (res.data.code === 0) {
            this.AgetCateList()
          }
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消!'
          })
        })
    },

    // 点击编辑按钮
    editFn (row) {
      this.dialogEditFormVisible = true
      this.formEdit = row
    },
    // 点击确定按钮
    editConfirmBtn () {
      this.$refs.EditForm.validate(async (valid) => {
        if (valid) {
          // 先回显数据，再展示对话框
          const res = await editCateByIdApi(this.formEdit)
          if (res.data.code === 0) {
            this.$message({
              message: '修改分类成功!',
              type: 'success'
            })
            //  3、刷新页面
            this.AgetCateList()
            // 4.关闭对话框
            this.dialogEditFormVisible = false
          }
        }
      })
    },
    // // 修改成功之后再次 重置编辑输入框
    resetEditForm () {
      this.$refs.EditForm.resetFields()
    }
  }
}
</script>

<style>
.addBtnLine {
  height: 44px;
  border-top: 1px solid #f3f5f7;
  margin-top: 7px;
}
.addBtn {
  float: right;
  margin-top: 6px;
}
</style>
